// page/details/pages/presentVerification/presentVerification.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ticketList: [],
    noData: true,
    isEnd: false,
    page: 1,
    show: false,
    // 单选框数组
    radioList: [],
    radio: ''
  },

  reqData() {
    const that = this;
    let token = app.globalData.token;
    //根据id领取优惠券
    let json = {
      token,
      page: 1,
      limit: 5
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.myHxGiftCoupon, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.status == 1) {

        that.setData({
          ticketList: res.result.paginate.data,
          page: 1
        })
        if (that.data.ticketList == '') {
          that.setData({
            noData: false
          })
        } else {
          that.setData({
            noData: true
          })
        }
      } else {
        throw res.msg;
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },
  reqcanhxData() {
    const that = this;
    this.setData({
      radio: '',
    }) 
    let token = app.globalData.token;
    //根据id领取优惠券
    let json = {
      token,
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.myCanHxGiftBox, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        var lists = res.result.list;
        if (lists.length) {
          that.setData({
            radioList: res.result.list,
            show: true
          })
        } else {
          wx.showToast({
            title: '您暂无权限，请联系管理员',
            icon: 'none',
            duration: 2000
          })
        }
      } else {
        throw res.msg;
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },
  onChange(event) {
    console.log(event.detail.value);
    this.setData({
      radio: event.detail.value,
    });
  },
  onClose() {
    this.setData({
      radio: '',
      show: false
    }) 
  },
  scanQr() {
    const that = this;
    if(!that.data.radio){
      wx.showToast({
        title: '请选择您要核销的赠品券',
        icon: 'none',
        duration: 2000
      })
      return
    }
    that.setData({
      show: false
    })
    wx.scanCode({
      success(res) {
        wx.showModal({
          content: '是否核销该赠品券',
          success(result) {
            if (result.confirm) {
              let arr = res.result.split('|');
              let token = app.globalData.token;
              let jsons = {
                item_id: that.data.radio,
                token,
                hx_code: arr[0],
                encry: arr[1]
              }
              let jsonDatas = app.utils.getSignData(jsons);
              wx.showLoading({
                title: '加载中...',
              })
              app.utils.post(app.api.reqUrls.hxGiftBox, jsonDatas).then(item => {
                wx.hideLoading();
                if (item.status == 1) {
                  wx.showToast({
                    title: item.msg,
                    icon: 'success',
                    duration: 2000
                  })
                  that.reqData()
                } else {
                  wx.showModal({
                    showCancel: false,
                    content: item.msg,
                    success(res) {
                      if (res.confirm) {
                      }
                    }
                  })
                }
              })
              wx.hideLoading();
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.reqData();
  },
  onShow: function () {
    //数据统计
    // app.statistics("page/detail/pages/myTicket/myTicket", "优惠券")
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;
    that.setData({
      page: 1,
      isEnd: false
    })
    wx.showNavigationBarLoading()
    that.reqData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    if (that.data.isEnd) {
      return
    }
    let token = app.globalData.token;
    //根据id领取优惠券
    let json = {
      token,
      page: that.data.page + 1,
      limit: 5
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.myHxGiftCoupon, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        if (res.result != '') {
          that.setData({
            ticketList: [...that.data.ticketList, ...res.result.paginate.data],
            page: that.data.page + 1
          })
        } else {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: true
          })
        }
      } else {
        throw res.msg;
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },

})